package com.kx.center.util;

import org.apache.log4j.Logger;

import cn.jpush.api.JPushClient;
import cn.jpush.api.common.APIConnectionException;
import cn.jpush.api.common.APIRequestException;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;

import com.kx.center.entity.MsgSend;

public class JpushUtil {

	private static final Logger logger = Logger.getLogger(JpushUtil.class);

	private static final String appKey = "c094ce5490f497a16547d645";
	private static final String masterSecret = "e320060165abaff5bb2941f7";

	static String ALERT = "你有一笔新的订单";
	static String TITLE = "多渠道订单";

	public static String SUGGESTION_REPLY = "您有一条意见反馈通知";
	
	public static boolean noticeMsgPush(String alias, String title,
			MsgSend msgSend) {

		JPushClient jpushClient = new JPushClient(masterSecret, appKey, 3);

		PushPayload payload = buildPushObject_android_tag_alertWithTitle(alias,
				title, msgSend);

//		PushPayload payload_ = buildPushObject_ios_tagAnd_alertWithExtrasAndMessage(
//				alias, title, msgSend);

		try {
			PushResult result = jpushClient.sendPush(payload);

			//PushResult result_ = jpushClient.sendPush(payload_);

			logger.info("Got result - " + result);
			return result.isResultOK() ;

		} catch (APIConnectionException e) {
			logger.error("Connection error, should retry later", e);

		} catch (APIRequestException e) {
			logger.error("Should review the error, and fix the request", e);
			logger.info("HTTP Status: " + e.getStatus());
			logger.info("Error Code: " + e.getErrorCode());
			logger.info("Error Message: " + e.getErrorMessage());
		}
		return false;
	}

	public static PushPayload buildPushObject_android_tag_alertWithTitle(
			String alias, String title2, MsgSend msgSend) {

		return PushPayload
				.newBuilder()
				.setPlatform(Platform.android())
				.setAudience(Audience.alias(alias))
				.setNotification(
						Notification
								.newBuilder()
								.addPlatformNotification(
										AndroidNotification
												.newBuilder()
												.setTitle(title2)
												.setAlert(alias)
												.addExtra("msgSendId",
														msgSend.getId())
												.addExtra("orderId",
														msgSend.getOrderId())
												.addExtra(
														"orderStatus",
														msgSend.getOrderStatus())
												.build()).build())
				.setMessage(Message.newBuilder().setMsgContent("").build())
				.build();
	}

	public static PushPayload buildPushObject_ios_tagAnd_alertWithExtrasAndMessage(
			String alias, String title, MsgSend msgSend) {
		return PushPayload
				.newBuilder()
				.setPlatform(Platform.ios())
				.setAudience(Audience.alias(alias))
				.setNotification(
						Notification
								.newBuilder()
								.addPlatformNotification(
										IosNotification
												.newBuilder()
												.setAlert(title)
												.addExtra("from", "JPush")
												.addExtra("msgSendId",
														msgSend.getId())
												.addExtra("orderId",
														msgSend.getOrderId())
												.addExtra(
														"orderStatus",
														msgSend.getOrderStatus())
												.build()).build())
				.setMessage(Message.content(title))
				.setOptions(
						Options.newBuilder().setApnsProduction(false).build())
				.build();
	}

	public static void newOrderPush(String mobile, String channel) {

		JPushClient jpushClient = new JPushClient(masterSecret, appKey, 3);

		PushPayload payload = buildPushObject_ios_audienceMore_messageWithExtras(
				mobile, channel);

		try {
			PushResult result = jpushClient.sendPush(payload);
			logger.info("Got result - " + result);

		} catch (APIConnectionException e) {
			logger.error("Connection error, should retry later", e);

		} catch (APIRequestException e) {
			logger.error("Should review the error, and fix the request", e);
			logger.info("HTTP Status: " + e.getStatus());
			logger.info("Error Code: " + e.getErrorCode());
			logger.info("Error Message: " + e.getErrorMessage());
		}
	}

	public static PushPayload buildPushObject_ios_audienceMore_messageWithExtras(
			String mobile, String channel) {
		return PushPayload
				.newBuilder()
				.setPlatform(Platform.android())
				.setAudience(Audience.alias(mobile))
				.setNotification(Notification.android(ALERT, TITLE, null))
				.setMessage(
						Message.newBuilder().setMsgContent("12345")
								.addExtra("type", channel).build()).build();
	}

}
